package com.web;


import com.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/messagelist")
public class MessageListController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/messagelist")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession(true);
        String message = request.getParameter("messagebox");
        String name = (String) session.getAttribute("nickname");

        if (message != null && name != null) {
            try {
                messageService.addMessage(message, name);
            } catch (SQLException | NamingException | IOException e) {

                e.printStackTrace();
            }
            return "chat";
        } else {
            return "start";
        }
    }
}
